// pages/goldmsg/goldmsg.js
const app=getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    payment:["停缴","缴存","未知"],
    idx:5,
    price:'',
    tel:"",
    wechat:"",
    pricePromt:false,
    telStatus:false,
    imgurl: "https://shuigongbao.witlending.com/img/pinkup/",
    url:"https://shuigongbao.witlending.com/img/pinkup/",
    userinfo:{},
    wechatStatus:false,
    upload:true,
    statusType: 1,
    pictureUrl:'/image/'
  },
  scroll(e){
    let scrollHeight = e.detail.scrollTop
    if(scrollHeight > 10){
      this.setData({
        background:'#FFA418'
      })
    }else{
      this.setData({
        background:'transparent'
      })
    }
  },
  // 获取手机号
  getPhoneNumber(e){
    let that = this;
    if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
      this.hot();
    } else {
      let iv = encodeURI(e.detail.iv)
      let encrypteddata = encodeURI(e.detail.encryptedData)
      let msg = {
        iv,
        encrypteddata
      }
      app.request('update_usertel', msg, true).then(res => {
        if (res.error_code == 0) {
          wx.setStorageSync('phone', true)
          this.setData({
            phone: true
          })
          this.hot();
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 验证是否授权手机号
    let phone = wx.getStorageSync('phone');
    if (phone != "") {
      this.setData({
        phone: phone
      })
    } else {
      wx.setStorageSync('phone', false)
      this.setData({
        phone: false
      })
    }

    let userinfo = app.globalData.userInfo
    this.setData({
      'userinfo.nickName': userinfo.nickName,
      'userinfo.avatarUrl': userinfo.avatarUrl
    })
    if(options.order_id){
    app.request("withdraw_cash/create", { order_id: options.order_id},true).then(res=>{
        if(res.error_code==0){
          this.setData({
            order_id:options.order_id,
            detail:res.data,
            idx: res.data.pay_status,
            tel: res.data.mobile,
            price: res.data.amount
          })
          if (options.city && options.code) {
            this.setData({
              'detail.area_name': options.city,
              'detail.area_code': options.code
            })
          }
        }
      })
    }
  },
  // 微信号
  wechat(e){
    this.setData({
      wechat: e.detail.value
    })
  },
  // 输入联系方式
  phone(e){
    this.setData({
      tel: e.detail.value.replace(/\D/g, '')
    })
  },
  // 选择公积金状态
  choose(){
    this.setData({
      statusType: 2
    })
  },
  choosed(){
    this.setData({
      statusType: 1
    })
  },
  // 选择公积金中心
  chooseCity(){
    wx.navigateTo({
      url: `/pages/providentfund/providentfund?payfor=true&orderid=${this.data.detail.order_id}`,
    })
  },

  // 火速提取
  hot(){
    let that=this;
    let flag=true;
    let flags=true;
    let indexFlag=true;
    let telflag=true;
    if (parseInt(this.data.price)>= 1000){
      this.setData({
        pricePromt: false
      })
      flags = true
    }else{
      this.setData({
        pricePromt: true
      })
      flags = false
    }

    if(this.data.tel.length!==11){
      this.setData({
        telStatus:true
      })
      telflag=false
    }else{
      this.setData({
        telStatus: false
      })
      telflag = true
    }

    if(this.data.wechat==""){
      this.setData({
        wechatStatus:true
      })
      flag=false
    }else{
      this.setData({
        wechatStatus:false
      })
      flag=true
    }

    if (flag && flags && telflag){
      let options = {
        order_id: this.data.detail.order_id,
        area_code: this.data.detail.area_code,
        mobile: this.data.tel,
        wechat:this.data.wechat,
        pay_status: this.data.statusType,
        amount: this.data.price
      }
      console.log(options)
      // return
      app.request("withdraw_cash/store", options, true).then(res => {
        app.aldstat.sendEvent('公积金提取-火速提取', '公积金提取-火速提取');
        wx.showLoading({
          title: '等待中',
        })
        if(res.error_code==0){
          this.setData({
            upload:false
          })
          wx.hideLoading()

          app.request("withdraw_cash/fire_speed",{order_id:that.data.detail.order_id},true).then(res=>{
            if(res.error_code==0){
              wx.reLaunch({
               url: `/pages/payment/payment?order_id=${that.data.detail.order_id}`

                // url: `../orderSubmission/orderSubmission?order_id=${that.data.detail.order_id}`,
              })
            }
          })
        }else{
          wx.hideLoading()

          wx.showModal({
            title: '温馨提示',
            content: '提交失败，请稍后重试',
            showCancel:false
          })
        }
      })
      
    }
  },
  // 同手机号
  instance(){
    this.setData({
      wechat:this.data.tel
    })
  },
  // 连线客服
  contact(){
    app.aldstat.sendEvent('公积金提取-联系客服', '公积金提取-联系客服');

  },
  // 获取提取金额
  price(e){
    this.setData({
      price: e.detail.value.replace(/\D/g, '')
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  saveMsg(){
    let detail={
      order_id: this.data.detail.order_id,
      area_code: this.data.detail.area_code,
      mobile: this.data.tel,
      wechat: this.data.wechat,
      pay_status: this.data.idx,
      amount: this.data.price
    }
    wx.setStorageSync("detail", detail);
    
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.saveMsg();
  },
  
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    this.saveMsg();
    // if (this.data.upload){
    //   let options = {
    //     order_id: this.data.detail.order_id,
    //     area_code: this.data.detail.area_code,
    //     mobile: this.data.tel,
    //     wechat: this.data.wechat,
    //     pay_status: this.data.idx,
    //     amount: this.data.price
    //   }
    //   app.request("withdraw_cash/store", options, true).then(res => {

    //   })
    // }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: "我正在查询公积金，快来查询吧",
      path: '/pages/index/index?channel=home',
      imageUrl: '../../https://head.image.jiajijie.com/dj/images/wuxianyijn.png',
      success: function (res) {
        wx.showShareMenu({
          withShareTicket: true
        })
        wx.showToast({
          title: '成功',
        })
        console.log(res)
      },
      fail: function (res) {
        console.log(res)
      }
    }
  }
})